<!DOCTYPE html >
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title th:text="${entityData.entityComment}"></title>
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">

    <link rel="stylesheet" href="../layui-v2.5.5/css/layui.css" media="all" />
    <link rel="stylesheet" href="../css/global.css" media="all">
    <link rel="stylesheet" href="../css/table.css" />
    <link rel="stylesheet" type="text/css" href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css">
</head>

<body>
	<div class="admin-main">
		<blockquote class="layui-elem-quote">
			<form class="layui-form">
				<div class="layui-form-item">
					<div class="layui-inline" th:each="entity : ${searchEntityAttrs}">
						<label class="layui-form-label" th:text="${entity.value}"></label>
						<div class="layui-input-inline">
							<input type="text" th:attr="name=${entity.key}" class="layui-input">
						</div>
					</div>
					<div class="layui-inline">
						<a href="javascript:;" class="layui-btn layui-btn-small" lay-submit lay-filter="search">
							<i class="layui-icon">&#xe615;</i> 搜索
						</a>
						<a href="javascript:;" class="layui-btn layui-btn-small layui-btn-danger" lay-submit lay-filter="reset">
							<i class="layui-icon">&#xe639;</i> 清除
						</a>
						<a href="javascript:;" class="layui-btn layui-btn-small" id="addBtn">
							<i class="layui-icon">&#xe608;</i> 新增
						</a>
						<a href="javascript:;" class="layui-btn layui-btn-danger layui-btn-small" id="delBtn">
							<i class="layui-icon">&#xe640;</i> 删除
						</a>
					</div>
				</div>
			</form>
		</blockquote>
		<fieldset class="layui-elem-field">
			<legend><span th:text="${entityData.entityComment}"></span>列表</legend>
			<div class="layui-field-box">
				<table class="site-table table-hover" id="dataTable" lay-filter="dataTable">

				</table>
			</div>
		</fieldset>
		<div class="admin-table-page">
			<div id="page" class="page">
			</div>
		</div>
	</div>
	<script type="text/javascript" src="../layui-v2.5.5/layui.js"></script>
	<script type="text/javascript" src="../js/base.js"></script>
	<script th:inline="javascript">

		layui.use(['laypage','layer','table','form'], function() {
			var $ = layui.jquery,
				laypage = layui.laypage,
				layer = layui.layer;
			var form = layui.form;

			var accessBaseUrl =[[${entityData.accessBaseUrl}]];

			var queryData={
				pageIndex:1,
				pageSize:10,
				sortName:[[${sortName}]],
				sortOrder:[[${sortOrder}]]
			};
			var checkbox = {checkbox:true, width:80};
			var dataIndex = {title: '序号', width:80, templet:function(row){
				return (queryData.pageIndex-1)*queryData.pageSize + row.LAY_TABLE_INDEX+1;
			}};
			var operates = {title: '操作', width:140, templet:function(row){
				var html='<a href="javascript:;" lay-event="edit" class="layui-btn layui-btn-mini">修改</a>'
					+'<a href="javascript:;" lay-event="del" class="layui-btn layui-btn-danger layui-btn-mini">删除</a>';
				return html;
			}};

			var datas = new Array();
			datas.push(checkbox);
			datas.push(dataIndex);

			var title = [[${entityData.entityComment}]];
			var primaryKey = [[${entityData.primaryKey}]];
			var entityName = [[${entityData.entityName}]];
			var entityAttrs = [[${entityData.entityAttrs}]];
			for(var item in entityAttrs){
				var obj = {
					field: item,
					title: entityAttrs[item],
					width:140
				};
				datas.push(obj);
			}
			datas.push(operates);

			var colsDatas = new Array();
			colsDatas.push(datas);

			var table = layui.table;
			var dataTable = table.render({
				elem: '#dataTable'
				,url: accessBaseUrl+entityName+'s'
				,where:queryData
				,headers:{
					Authorization:Authorization
				}
				,method:'GET'
				,request: {
					pageName: 'pageIndex'
					,limitName: 'pageSize'
				}
				,parseData: function(res){
					return {
						'code':(res.code==200?0:-1),
						'msg': res.message,
						'count': res.data?res.data.total:0,
						'data': res.data?res.data.records:[]
					};
				}
				,autoSort:false
				,cellMinWidth: 100
			    ,cols: colsDatas,
				done: function(res, curr, count){
					laypage.render({
						elem: 'page',
						count: count,
						limit: queryData.pageSize,
						curr: queryData.pageIndex,
						groups:5,
						limits: [10, 20, 30, 40, 50],
						jump: function(obj, first) {
							var curr = obj.curr;
							if(!first) {
								reloadData(curr);
							}
						}
					});
				}
			});

			function reloadData(pIndex,params){
				queryData.pageIndex = pIndex?pIndex:1;
				if(params){
					for(var key in params){
						queryData[key] = params[key];
					}
				}
				table.reload('dataTable', {
				    where: queryData
				});
			}

			table.on('tool(dataTable)', function(obj){
			  switch(obj.event){
			    case 'edit':
			      editEntity(obj.data[primaryKey]);
			    break;
		      	case 'del':
		      	  var ids = [];
		      	  ids.push(obj.data[primaryKey]);
			      delEntity(ids,"确认删除该项吗？");
			    break;
			  };
			});

			$('#delBtn').on('click', function() {
				var checkStatus = table.checkStatus('dataTable');
				if(checkStatus.data.length==0){
					layer.msg("请选择一行！",{icon:0});
					return;
				}
				var ids = [];
				for(var i=0;i<checkStatus.data.length;i++){
					ids.push(checkStatus.data[i][primaryKey]);
				}

				if(ids.length<=0){
					layer.msg("请选择一行！",{icon:0});
					return;
				}
				delEntity(ids,"确认删除所选项吗？");
			});

			function delEntity(ids,message){
				layer.confirm(message,{icon:3,title:'提示信息'},function(index){
					layer.close(index);
					var load = layer.load(2);

					$.ajax({
		            	url:accessBaseUrl+entityName+'s',
		            	type:'DELETE',
		            	data:JSON.stringify(ids),
	            		contentType: 'application/json',
		            	cache: false,
		            	async:true,
		            	headers:{
				    		Authorization:Authorization
				    	},
		            	success:function(res){
		            		if(res.code == 200){
								layer.msg(res.message,{icon:1, time:500},function (){
									reloadData(1);
								});
							}
		            		layer.close(load);
		            	}
		            });
				});
			}

			$('#addBtn').on('click', function() {
				layer.open({
					type:2,
					title:'新增'+title,
					area: ['660px', '550px'],
					content:'add'
				});
			});

			function editEntity(id){
				layer.open({
					type:2,
					title:'编辑'+title,
					area: ['660px', '550px'],
					content:'edit?id='+id
				});
			}

			form.on('submit(search)', function(data) {
				var result = data.field;
				reloadData(1,result);
			});

			form.on('submit(reset)', function(data) {
				$(data.form).find("input").val("");
				form.render();

				var result = data.field;
				for(var key in result){
					result[key] = '';
				}
				reloadData(1,result);
			});
		});
	</script>
</body>

</html>
